<#include "header.ftl" />
<body>
<header class="am-topbar am-topbar-fixed-top">
  <div class="am-container">
    <h1 class="am-topbar-brand">
      <a href="#">聊天室</a>&nbsp;联系人：<input type="text" id="sendId" value="发送到。。"></input>
    </h1>
    <div class="am-collapse am-topbar-collapse" id="collapse-head">
      <ul class="am-nav am-nav-pills am-topbar-nav">
      
        
      </ul>
      <div class="am-topbar-right">
        <button class="am-btn am-btn-secondary am-topbar-btn am-btn-sm"><span class="am-icon-pencil"></span> 注册</button>
      </div>
      <div class="am-topbar-right">
        <button class="am-btn am-btn-primary am-topbar-btn am-btn-sm"><span class="am-icon-user"></span> 登录</button>
      </div>
      <div class="am-topbar-right">
        <button class="am-btn am-btn-primary am-topbar-btn am-btn-sm"><span class="am-icon-user"></span> 退出</button>
      </div>
    </div>
  </div>
</header>
<div id="main">
    <!-- 聊天内容展示区域 -->
    <div id="ChatBox" class="am-g am-g-fixed" >
      <div class="am-u-lg-12" style="height:400px;border:1px solid #999;overflow-y:scroll;">
        <ul id="chatContent" class="am-comments-list am-comments-list-flip">
          
            <li id="msgtmp" class="am-comment" style="display:none;">
  
    <div class="am-comment-main" >
        <header class="am-comment-hd">
            <div class="am-comment-meta">
              <a ff="nickname" href="#link-to-user" class="am-comment-author">某人</a>
              
              <time ff="msgdate" datetime="" title="">2014-7-12 15:30</time>
            </div>
        </header>
     <div ff="content" class="am-comment-bd">此处是消息内容</div>
    </div>
</li>
        </ul>
      </div>
    </div>
    <!-- 聊天内容发送区域 -->
    <div id="EditBox" class="am-g am-g-fixed">
    <!--style给定宽度可以影响编辑器的最终宽度-->
    <script type="text/plain" id="myEditor" style="width:100%;height:140px;"></script>
    <button id="send" type="button" class="am-btn am-btn-primary am-btn-block">发送</button>
    </div>
    </div>
    <script type="text/javascript">
    var um = "";
    $(function(){
    //窗口大小变化时，调整显示效果
    $("#ChatBox div:eq(0)").height($(this).height()-290);
    $(window).resize(function(){
        $("#ChatBox div:eq(0)").height($(this).height()-290);
    });
    //实例化编辑器
    um = UM.getEditor('myEditor',{
        initialContent:"请输入聊天信息...",
        autoHeightEnabled:false,
        toolbar:[
            'undo redo | bold italic underline strikethrough | superscript subscript | forecolor backcolor | removeformat |',
            'insertorderedlist insertunorderedlist | selectall cleardoc paragraph | fontfamily fontsize' ,
            '| justifyleft justifycenter justifyright justifyjustify |',
            'emotion'
        ]
    });});
   
    function addMessage(msg){
    	  var box = $("#msgtmp").clone();     //复制一份模板，取名为box
    	    box.show();                         //设置box状态为显示
    	    box.appendTo("#chatContent");       //把box追加到聊天面板中
    	    box.find('[ff="nickname"]').html(msg.name); //在box中设置昵称
    	    box.find('[ff="msgdate"]').html(msg.date);      //在box中设置时间
    	    box.find('[ff="content"]').html(msg.content);   //在box中设置内容
    	    box.addClass(msg.isSelf? 'am-comment-flip':''); //右侧显示
    	    box.addClass(msg.isSelf? 'am-comment-warning':'am-comment-success');//颜色
    	    box.css((msg.isSelf? 'margin-left':'margin-right'),"20%");//外边距
    	    $("#ChatBox div:eq(0)").scrollTop(999999);  //滚动条移动至最底部
    }
  //自己的昵称
    var nickname ="${privateIds!'000000000'}";
    //建立一条与服务器之间的连接
    var socket = new WebSocket("ws://192.168.199.169:8081/onclickwebchat/webSocket?privateId="+nickname);
    //接收服务器的消息
    socket.onmessage=function(ev){
        var obj = eval(   '('+ev.data+')'   );
        addMessage(obj)
    }
    //发送按钮被点击时
    $("#send").click(function(){
        if (!um.hasContents()) {  // 判断消息输入框是否为空
            // 消息输入框获取焦点
            um.focus();
            // 添加抖动效果
            $('.edui-container').addClass('am-animation-shake');
            setTimeout("$('.edui-container').removeClass('am-animation-shake')", 1000);
        } else {
            //获取输入框的内容
            var txt = um.getContent();
            var uid = $("#sendId").val();
            //构建一个标准格式的JSON对象
            var obj = JSON.stringify({
                name:"张三"+uid,
                content:txt,
                privateId:uid,
                isFlock:"false"
            });
            // 发送消息
           
            addMessage(eval(   '('+obj+')'   ));
            socket.send(obj);
            // 清空消息输入框
            um.setContent('');
            // 消息输入框获取焦点
            um.focus();
        }
    });
    
    </script>
</body>
</html>